using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:AcmsImageLink runat=server></{0}:AcmsImageLink>")]
    public class AcmsImageLink : HyperLink
    {
        private int? fileId;
        public int? FileId
        {
            get { return fileId; }
            set { fileId = value; }
        }

        private int renderHeight;
        public int RenderHeight
        {
            get { return renderHeight; }
            set { renderHeight = value; }
        }

        private int renderWidth;
        public int RenderWidth
        {
            get { return renderWidth; }
            set { renderWidth = value; }
        }


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (FileId != null && FileId.Value > 0)
            {
                this.ImageUrl = string.Format("~/File.aspx?Id={0}&width={1}&height={2}", FileId.Value, RenderWidth, RenderHeight);
                this.Visible = true;
                base.Render(writer);
            }
            else
            {
                this.Visible = false;
            }
        }

        protected override object SaveViewState()
        {
            ViewState["FileId"] = fileId;
            ViewState["RenderWidth"] = renderWidth;
            ViewState["RenderHeight"] = renderHeight;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["FileId"] is int?)
            {
                fileId = (int?)ViewState["FileId"];
            }
            if (ViewState["RenderWidth"] is int)
            {
                renderWidth = (int)ViewState["RenderWidth"];
            }
            if (ViewState["RenderHeight"] is int)
            {
                renderHeight = (int)ViewState["RenderHeight"];
            }
        }
    }
}
